package com.life.smile.service.Impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.life.smile.dao.MenuMapper;
import com.life.smile.entity.RearMenu;
import com.life.smile.entity.Role;
import com.life.smile.entity.vo.RearMenuVo;
import com.life.smile.entity.vo.UserLoginLogVo;
import com.life.smile.service.MenuService;


@Service("MenuService")
public class MenuServiceImpl implements MenuService{

	@Autowired
	private MenuMapper menuMapper;
	
	
	/**
	 * 获取所有的菜单
	 * @return 
	 */
	@Override
	public List<RearMenuVo> getAllMenu() {
		return menuMapper.getAllMenu();
	}

	/**
	  * 查询所有的1.2级目录，用于添加菜单页面的表单回填
	  * @return
	  */
	@Override
	public List<RearMenu> getParentMenu() {
		return menuMapper.getParentMenu();
	}

	/**
	 * 添加菜单
	 */
	@Override
	public void createMenu(RearMenu m) {
		int level = menuMapper.getMenuLevel(m.getParentid());
		m.setLevel(level+1);
		menuMapper.createMenu(m);
		
	}

	
	/**
	 * 删除菜单
	 * @param mid
	 */
	public void menuDelete(Integer mid){
		menuMapper.menuDelete(mid);
	}

	/**
	 * 获取所有的角色
	 */
	@Override
	public List<Role> getRoleList() {
		return menuMapper.getRoleList();
	}
	
	/**
	 * 检查菜单是否存在
	 * @param url
	 * @return 存在返回success
	 */
	public String checkMenu(String url){
		RearMenu menu = menuMapper.checkMenu(url);
		if(menu != null){
			return "success";
		}
		return "fair";
	}

	@Override
	public RearMenuVo getMenu(Integer mid) {
		return menuMapper.getMenu(mid);
	}

	@Override
	public void valueUpdata(RearMenu m) {
		if(m.getParentid() != null){
			int level = menuMapper.getMenuLevel(m.getParentid());
			m.setLevel(level+1);
		}
		menuMapper.valueUpdata(m);
	}
	
	
	/**
	 * 查询所有的菜单列表
	 * @return
	 */
	public List<RearMenu> searchMenuAll(){
		return menuMapper.searchMenuAll();
	}

	/**
	 * 查询角色的菜单列表
	 * @param rid
	 * @return
	 */
	public List<RearMenu> searchMenuByRoleId(Integer rid){
		return menuMapper.searchMenuByRoleId(rid);
	}
	
	/**
	 * 修改角色拥有的菜单
	 * @param menu_id_str
	 */
	@Transactional
	public void updateRoleMenu(String menu_id_str,Integer rid){
		menuMapper.deleRoleMenu(rid);//删除角色所有菜单
		String [] midstr = null;
		midstr = menu_id_str.split("_");//去除下划线，得到菜单id数组
		Map<String, Integer> map = new HashMap<String,Integer>();
		for(int i = 0 ; i < midstr.length; i++){
			System.out.println(midstr[i]);
			Integer mid = Integer.valueOf(midstr[i]);//获取到当前菜单id
			map.put("rid", rid);
			map.put("mid", mid);
			menuMapper.setRoleMenu(map);//数据库执行插入语句
			map.clear();//清空map
		}
	}

	/**
	 * 判断当前用户是否有当前请求的权限
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public boolean searchMenuByUser(Integer userType, String auth) {
		Map paraMap = new HashMap();
		paraMap.put("userType", userType);
		paraMap.put("auth", auth);
		Map authMap = menuMapper.searchMenuByUser(paraMap);
		return authMap!=null?true:false;
	}
	
	/**
	 * 查询角色是否存在
	 * @param rname
	 * @return
	 */
	public boolean checkRole(String rname){
		Role r = menuMapper.checkRole(rname);
		if(r != null){
			return true;//存在
		}
		return false;
	}
	
	
	/**
	 * 添加角色
	 * @param role
	 */
	public void createRole(Role role){
		menuMapper.createRole(role);
	}

	@Override
	public void roleDelete(Integer rid) {
		menuMapper.roleDelete(rid);
	}


	@Override
	public List<Role> getRoleName() {
		return menuMapper.getRoleName();
	}

	@Override
	public List<UserLoginLogVo> loginLogList(Map<String, Object> map) {
		return menuMapper.loginLogList(map);
	}

	@Override
	public Integer getCount(Map<String, Object> map) {
		return menuMapper.getCount(map);
	}

	@Override
	public List<RearMenuVo> selectAllMenuList(Map<String, Object> pmap) {
		return menuMapper.selectAllMenuList(pmap);
	}

	@Override
	public int getMenuCount(Map<String, Object> pmap) {
		return menuMapper.getMenuCount(pmap);
	}
	
	
	
	
	
}
